{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (icesat2data.py, line 571)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"/Users/ellen.buckley/icepyx/icepyx/core/icesat2data.py\"\u001b[0;36m, line \u001b[0;32m571\u001b[0m\n\u001b[0;31m    capability_url = f'https://n5eil02u.ecs.nsidc.org/egi/capabilities/{self.dataset}.{self._version}.xml'\u001b[0m\n\u001b[0m                                                                                                         ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import xarray as xr\n",
    "import pandas as pd\n",
    "import h5py\n",
    "import os,json, sys\n",
    "from pprint import pprint\n",
    "sys.path.append('/Users/ellen.buckley/icepyx/')\n",
    "from icepyx import icesat2data as ipd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### This is just a short example. Check the full subsetting notebook below for more details. \n",
    "https://github.com/icesat2py/icepyx/blob/master/doc/examples/ICESat-2_DAAC_DataAccess2_Subsetting.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialize the data request\n",
    "Provide the product name, bounding box (lon/lat of the lower left and upper right corner in lat/lon grid), date, and time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ipd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-1feade0c22e7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0matl07\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mipd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIcesat2Data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ATL07'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m170\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m70\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m130\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m80\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'2019-02-22'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'2019-02-23'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m                            \u001b[0mstart_time\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'00:00:00'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend_time\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'23:59:59'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'ipd' is not defined"
     ]
    }
   ],
   "source": [
    "atl07 = ipd.Icesat2Data('ATL07',[-170, 70, -130, 80],['2019-02-22','2019-02-23'], \\\n",
    "                           start_time='00:00:00', end_time='23:59:59')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.earthdata_login('user name','email')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use the following command to show the variable dictionary. \n",
    "Set `dictview=False` to show in the group directory view. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.show_custom_options(dictview=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List all the available variables but not the actual paths for simplicity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_vars.avail(options=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add a default list of variables to the data request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_vars.append(defaults=True)\n",
    "pprint(atl07.order_vars.wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: build the list provided by Ellen as the user defined list of variables\n",
    "\n",
    "* Group: `gtx/sea_ice_segments`\n",
    " * `latitude`\n",
    " * `longitude`\n",
    " * `geoseg_beg`\n",
    " * `geoseg_end`\n",
    "* Group: `gtx/sea_ice_segments/heights`\n",
    " * `height_segment_height`\n",
    " * `height_segment_confidence`\n",
    " * `height_segment_surface_error_est`\n",
    " * `height_segment_type`\n",
    " * `height_segment_ssh_flag`\n",
    " * `height_segment_w_gaussian`\n",
    "* Group: `/gtx/sea_ice_segments/stats`\n",
    " * `photon_rate`\n",
    " * `cloud_flag_asr`\n",
    " * `exmax_mean_1`\n",
    " * `exmax_mean_2`\n",
    " * `height_coarse_stdev`\n",
    " * `n_photon_used`\n",
    " * `trim_height_bottom`\n",
    " * `trim_height_top`\n",
    " * `sc_orient`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List of predefined list of default variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pprint(atl07.order_vars.wanted.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Remove the variables not in the user specified list "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_vars.remove(var_list=['height_segment_length_seg','height_segment_quality','seg_dist_x'])\n",
    "pprint(atl07.order_vars.wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add missing variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_vars.append(var_list=[\n",
    "    'geoseg_beg','geoseg_end',\n",
    "    'height_segment_surface_error_est','height_segment_w_gaussian',\n",
    "    'photon_rate','cloud_flag_asr','exmax_mean_1','exmax_mean_2',\n",
    "    'height_coarse_stdev','n_photons_used','trim_height_bottom',\n",
    "    'trim_height_top'])\n",
    "pprint(atl07.order_vars.wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Refine to remove weak beams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_vars.remove(beam_list=['gt1r','gt2r','gt3r'])\n",
    "pprint(atl07.order_vars.wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comments\n",
    "* It is much easier than the example with ATL09, because no data with the same variable name under different beams/subdirectories in ATL07. \n",
    "* ATL10 will be very different. In v02, over 30 `latitude`. If we want to filter what we need, combination of `beam_list` and `keyword_list` is needed. Check the main subsetting notebook examples. \n",
    "* Need to run append/remove a couple of times to get what you need, but still way faster than doing this by hand. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pass the variable list to order data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.order_granules(Coverage=atl07.order_vars.wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A list of `orderIDs` will be saved in `atl07.granules.orderIDs`. Save this list to restart your download if it gets interrupted. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.granules.orderIDs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atl07.download_granules('down')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "old_dir = 'down'\n",
    "new_dir = 'ATL07/example/'\n",
    "if not os.path.exists(new_dir): os.makedirs(new_dir)\n",
    "for root, dirs, files in os.walk(old_dir, topdown=False):\n",
    "    for file in files:\n",
    "        try:\n",
    "            shutil.move(os.path.join(root, file), os.path.join(new_dir,file))\n",
    "        except OSError:\n",
    "            pass\n",
    "        \n",
    "for root, dirs, files in os.walk(old_dir):\n",
    "    for name in dirs:\n",
    "        os.rmdir(os.path.join(root, name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "varname = 'latitude'\n",
    "#varname = 'sc_orient'\n",
    "\n",
    "varlist = []\n",
    "def IS2h5walk(vname, h5node):\n",
    "    if isinstance(h5node, h5py.Dataset):\n",
    "        varlist.append(vname)\n",
    "    return \n",
    "\n",
    "with h5py.File(fn,'r') as h5pt:\n",
    "    h5pt.visititems(IS2h5walk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
